#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "args.hpp"
#include "word.hpp"
#include "strlib.hpp"
#include "main.hpp"

using namespace std;

bool debugEnabled;

vector<Word*> loadedWords;
vector<string> languages;
bool testAllWords;
int numberOfWordsToTest;

void loadWordsFromFiles(vector<string> files, vector<Word*>* wordVector) {
  for(int i = 0; i < files.size(); i++) {
    string line;
    ifstream file;
    file.open(files[i]);
    if(file.is_open()) {
      while(getline(file, line)) {
        wordVector->push_back(new Word(line, debugEnabled));
      }
    }
  }
}

void testUser(vector<Word*> availableWords, bool testAll, int amountToTest = 1, float treshold = 100) {
  vector<int> toTest;
  int amount;
  float score = 0;
  if(testAll) {
    amount = availableWords.size();
    for(int i = 0; i < amount; i++) {
      toTest.push_back(i);
    }
  } else {
    if (amountToTest > availableWords.size()) {
      amount = availableWords.size();
    } else {
      amount = amountToTest;
    }
    for(int i = 0; i < amount; i++) {
    generateNumber:int choice = rand() % availableWords.size();
      for(int e = 0; e < toTest.size(); e++) {
        if(choice == toTest[e]) {
          goto generateNumber;
        }
      }
      toTest.push_back(choice);
    }
  }
  for(int i = 0; i < toTest.size(); i++) {
    if (availableWords[toTest[i]]->percentage < treshold) {
      score += availableWords[toTest[i]]->testByLang(languages[0], languages[1]);
    }
  }

  cout << "Your score is " << score << "/" << amount << " or " << ((float)score/(float)amount) * 100 << "%" << "\n";
}

void testUserInfinite() {
  while(true) {
    testUser(loadedWords, true, NULL, 0.7);
    sortByScore(&loadedWords);
    for(int i = 0; i < loadedWords.size(); i++) {
      cout << loadedWords[i]->percentage << "\n";
      if(loadedWords[i]->percentage < 0.7) {
	goto continueTestInf;
      }
    }
    break;
  continueTestInf:;
  }
}

int main(int argc, char **argv) {
  srand(time(NULL));
  debugEnabled = false;

  numberOfWordsToTest = 0;
  testAllWords = true;
  debugEnabled = false;

  if(getArgExists(argc, argv, "--debug-enable", true)) {
    debugEnabled = true;
    cout << "Enabled debug mode" << "\n";
  }

  if(getArgExists(argc, argv, "-editDistance")) {
    auto strings = getArgValues(argc, argv, "-editDistance");
    if(strings.size() < 2) {
      cout << "You did not enter two strings to compare" << "\n";
    } else {
      cout << editDistance(strings[0], strings[1]) << "\n";
    }
  }
  if(getArgExists(argc, argv, "-compare")) {
    auto strings = getArgValues(argc, argv, "-compare");
    if(strings.size() < 2) {
      cout << "You did not enter two strings to compare" << "\n";
    } else {
      cout << compareStrings(strings[0], strings[1]) << "\n";
    }
  }


  if (getArgExists(argc, argv, "-d", debugEnabled)) {
    loadWordsFromFiles(getArgValues(argc, argv, "-d", debugEnabled), &loadedWords);
  } else {
    return 0;
  }
  if (getArgExists(argc, argv, "-l", debugEnabled)) {
    languages = getArgValues(argc, argv, "-l", debugEnabled);
  } else {
    return 0;
  }
  if (getArgExists(argc, argv, "-n", debugEnabled)) {
    auto args = getArgValues(argc, argv, "-n", debugEnabled);
    testAllWords = false;
    if(args[0] == "all") {
      numberOfWordsToTest = loadedWords.size();
    } else {
      numberOfWordsToTest = stoi(args[0]);
    }
  } else {
    return 0;
  }
  if(getArgExists(argc, argv, "-t", debugEnabled)) {testUser(loadedWords, testAllWords, numberOfWordsToTest);}
  if(getArgExists(argc, argv, "-ti", debugEnabled)) {testUserInfinite();}
}
